home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-11-10 | 2.6 KB | 85 lines | [TEXT/GEOL] |
- Item 9874114 6-Nov-89 20:51
-
- From: PASCOE1 Pascoe, Geoff
-
- To: MACAPP.TECH$ MacApp Technical
-
- Sub: More on Failure Handlers
-
- Larry Goldman,
-
- Well, you called me on it! Yep, your right! In the initialization method the
- MacApp convention for allocating seems to be to set up a failure handler. So,
- the routine that called New and IWhatever must be prepared to deal with an
- object handle that is no longer valid. Apparently, if the routine is another
- initialization method the object is created into a temporary (you have to do
- this anyway since SELF may move when NEW is called) so if the initialization
- fails the calling initialization will unroll from that point leaving the
- original instance variable still NIL. That is,
-
- TFoo.IFoo;
-
- VAR
-
- aBar : TBar;
- fi : FailInfo;
-
- FooHandler(.....);
-
- BEGIN {FooHandler}
- SELF.Free
- END; {FooHandler}
-
- BEGIN {TFoo.IFoo}
- fBar := NIL; (* We'll want this to point to a TBar. *)
-
- INHERITED ISuperFoo; (* If we fail here we're OK if in Free we *)
- (* FreeIfObject(fBar). *)
-
- CatchFailures(fi, FooHandler)
- NEW(aBar);
- FailNIL(aBar);
- aBar.IBar; (* If we fail here aBar will reclaim itself. *)
- fBar := aBar; (* Since we set fBar only after we succeed *)
- Success(fi) (* TFoo.Free won't try to free the instance of *)
- (* TBar twice. *)
- END; {TFoo.IFoo}
-
- TBar.IBar
-
- VAR
-
- aBlob : Handle;
- fi : FailInfo;
-
- BarHandler(.......)
-
- BEGIN {BarHandler}
- DisposIfHandle(aBlob);
- SELF.Free
- END; {BarHandler}
-
- BEGIN {TBar.IBar}
- fBlob := NIL;
-
- INHERITED ISuperBar;
-
- CatchFailures(fi, BarHandler);
- aBlob := NewHandle(someSize); (* Don't assign to instVar here *)
- fBlob := aBlob (* since SELF can move. *)
- Success(fi)
-
- END; {TBar.IBar}
-
- Another POSSIBLE convention would be to have all the creating procedures
- responsible for freeing. In this case IFoo would free aBar and the caller of
- IFoo would free the Foo. That way the handle is always valid but the
- responsibility is pushed one level out. I don't think this is quite as nice as
- the current scheme. Did I get it right this time?
-
- Incidentally, is the failure handling mechanism fully reentrant? That is, can
- I set up a failure handler inside a failure handler?
-
- Geoff
-
-